﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICarPool.DTO;
using ICarPool.DAL.Factory;
using ICarPool.DAL.Interface;
using ICarPool.ORM.EntityManager;
using ICarPool.ORM.DBUtility;
using System.Data;
using ICarPool.BLL.Common;

namespace ICarPool.BLL
{
    public class CarPoolUserBLL
    {
        IEntityManager manager;

        public CarPoolUserBLL()
        {
            manager = EntityManagerFactory.CreateEntityManager();
        }

        #region 登陆
        /// <summary>
        /// 验证用户登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public EnumLoginResult Login(string loginName, string password)
        {
            CarPoolUser user = GetUserByLoginName(loginName);

            if (user == null)
            {
                return EnumLoginResult.NoRecord;
            }
            else
            {
                if (user.PassWord == password)
                {
                    return EnumLoginResult.Success;
                }
                else
                {
                    return EnumLoginResult.Failed;
                }
            }
        }

        /// <summary>
        /// 根据登录名，获取用户信息
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        private CarPoolUser GetUserByLoginName(string loginName)
        {
            CarPoolUser user = null;

            List<CarPoolUser> listUser = manager.FindByCondition<CarPoolUser>(string.Format(@"LoginName='{0}'", loginName));
            if (listUser.Count > 0)
            {
                user = listUser[0];
            }

            return user;
        } 
        #endregion

        #region 新建用户
        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public int Save(CarPoolUser user)
        {
            return manager.Save<CarPoolUser>(user);
        } 
        #endregion

        #region 更新用户
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int Update(CarPoolUser user)
        {
            return manager.Update<CarPoolUser>(user);
        } 
        #endregion
    }
}
